package com.swordofferII.xiepanpan;

import org.junit.Test;

/**
 * @program: leetcode
 * @description:
 * @author: wangzhihua
 * @date: 2024-03-20
 */
public class ArrySumEq20 {

	int sum = 20;
	int[] nums = new int[]{1, 1, 1, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11};

	void findSum(int result, int i, String indexs) {
		if (i >= nums.length) {
			return;
		}
		i += 1;
		if (result > sum) {
			return;
		}
		if (result == sum) {
			String[] indexArray = indexs.replaceFirst(",", "").split(",");
			String out = "";
			for (String index : indexArray) {
				out += nums[Integer.valueOf(index)] + " + ";
			}
			out = out.substring(0, out.length() - 2) + "= " + sum;
			System.out.println(out);
			return;
		}
		findSum(result, i, indexs);
		if (i >= nums.length) {
			return;
		}
		findSum(result + nums[i], i, indexs + "," + i);
	}

	@Test
	public void test() {
		findSum(0, -1, "");
	}

}
